﻿using System;
using JetBrains.Annotations;

namespace Kaleida.ServiceMonitor.Model.StructuredSource
{
    public class ImpersonationDefinition
    {
        private readonly string domain;
        private readonly string username;
        private readonly string password;

        public ImpersonationDefinition([NotNull] string domainAndUsername, [NotNull] string password)
        {
            if (domainAndUsername == null) throw new ArgumentNullException("domainAndUsername");
            if (password == null) throw new ArgumentNullException("password");

            var components = domainAndUsername.Split('\\');
            switch (components.Length)
            {
                case 1:
                    domain = "";
                    username = components[0];
                    break;
                case 2:
                    domain = components[0];
                    username = components[1];
                    break;
                default:
                    throw new ArgumentOutOfRangeException("domainAndUsername", domainAndUsername, "Expected value to contain 'domain\\username' or just 'username'");
            }

            this.password = password;
        }

        public ImpersonationDefinition([NotNull] string domain, [NotNull] string username, [NotNull] string password)
        {
            if (domain == null) throw new ArgumentNullException("domain");
            if (username == null) throw new ArgumentNullException("username");
            if (password == null) throw new ArgumentNullException("password");

            this.domain = domain;
            this.username = username;
            this.password = password;
        }

        public string Domain
        {
            get { return domain; }
        }

        public string Username
        {
            get { return username; }
        }

        public string Password
        {
            get { return password; }
        }

        public override string ToString()
        {
            if (domain == "")
                return username;

            return string.Format("{0}\\{1}", domain, username);
        }
    }
}
